package com.minimalist.common.config.javamelody;

import jakarta.servlet.Filter;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 11987
 */
@Configuration
public class MonitoringConfig{

    @Bean
    public FilterRegistrationBean<Filter> monitoringFrameOptionsFilter(){
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter((request, response, chain) -> {
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            // 允许所有域名的iframe嵌入（生产环境建议限制特定域名）
            httpResponse.setHeader("X-Frame-Options", "ALLOWALL");
            chain.doFilter(request, response);
        });
        registration.addUrlPatterns("/monitoring");
        registration.addUrlPatterns("/monitoring/*");
        registration.setName("monitoringFrameOptionsFilter");
        registration.setOrder(1);
        return registration;
    }
}